//
//  ChatToolsView.swift
//  七哥直播
//
//  Created by seven on 2017/12/25.
//  Copyright © 2017年 seven. All rights reserved.
//

import UIKit

protocol ChatToolsViewDelegate: class {
    func chatToolsView(toolView: ChatToolsView, message: String)
}

class ChatToolsView: UIView,NibLoadable {
    weak var delegate: ChatToolsViewDelegate?
    @IBOutlet weak var inputTextField: UITextField!
    @IBOutlet weak var sendMsgBtn: UIButton!
    private lazy var emoticonBtn : UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
    private lazy var emoticonView: EmoticonView = EmoticonView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 260 + kBottomSafeArea))
    override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
    
    
}

//MARK:- UI相关
extension ChatToolsView{
    private func setupUI(){
        // 1.初始化inputView中rightView
        emoticonBtn.setImage(UIImage(named: "chat_btn_emoji"), for: .normal)
        emoticonBtn.setImage(UIImage(named: "chat_btn_keyboard"), for: .selected)
        emoticonBtn.addTarget(self, action: #selector(emoticonBtnClick(_:)), for: .touchUpInside)

        inputTextField.rightView = emoticonBtn
        inputTextField.rightViewMode = .always
        inputTextField.allowsEditingTextAttributes = true
        
        //2.设置表情view的闭包
        emoticonView.emoticonClickCallback = { [weak self] emoticon in
            //1.判断是否是删除按钮
            if emoticon.emoticonName == "delete-n"{
                self?.inputTextField.deleteBackward()
                return
            }
            //2.获取光标位置
            guard let range = self?.inputTextField.selectedTextRange else {return}
            self?.inputTextField.replace(range, withText: emoticon.emoticonName)
        }
    }
}

//MARK:- 事件监听
extension ChatToolsView{
    @IBAction func textFieldEditingChanged(_ sender: UITextField) {
        sendMsgBtn.isEnabled = Int(sender.text!.count) != 0
    }
    
    @IBAction func sendBtnClick(_ sender: UIButton) {
        //1.获取内容
        let message = inputTextField.text!
        //2.清空内容
        inputTextField.text = ""
        sender.isEnabled = false
        //3.将内容回调出去
        delegate?.chatToolsView(toolView: self, message: message)
    }
    
    @objc private func emoticonBtnClick(_ btn : UIButton) {
        btn.isSelected = !btn.isSelected
        
        // 切换键盘
        let range = inputTextField.selectedTextRange
        inputTextField.resignFirstResponder()
        inputTextField.inputView = inputTextField.inputView == nil ? emoticonView : nil
        inputTextField.becomeFirstResponder()
        inputTextField.selectedTextRange = range
    }
}
